<?php 
$GLOBALS['Md'] = "TimeClock";
$GLOBALS['Pd'] = "B";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
print heade(); 
print body(); 
module($BSD['DB00']);
office_header($_SESSION[$SESSOF]);
include($GLOBALS['PAD']."Modules/TimeClock/main.obj.php");
include($GLOBALS['PAD']."Modules/TimeClock/nav.views.php");
include($GLOBALS['PAD']."Shared/Libraries/Objects/sched.calend.php");
print NavBack();

	// Vars
	if (isset($_POST['Event'])) {
		$_GET['Event'] = $_POST['Event'];
		}
	if (isset($_POST['FlType'])) {
		$_GET['EventType'] = $_POST['FlType'];
		}
	if (isset($_POST['FlStatus'])) {
		$_GET['Status'] = $_POST['FlStatus'];
		}
	if (isset($_POST['FlOffice'])) {
		$_GET['Offices'] = $_POST['FlOffice'];
		}
	if (isset($_POST['Month'])) {
		$_GET['Month'] = $_POST['Month'];
		}
	if (!isset($_GET['EventType'])) {
		$_GET['EventType'] = 1;
		}

if (isset($_POST['Save']) && isset($_POST['Status'])) {

	$Status = $_POST['Status'];
	$Count = count($Status);
	for ($r = 0; $r < $Count; $r++) {
		$STA = split('-',$Status[$r]);
		if (isset($STA[2])) {
			if (isset($STA[0]) && $STA[0] < 4) {
				$QValidate = "UPDATE timeclock_delays 
				SET Status = {$STA[2]}
				WHERE ref = {$STA[1]}";
				$GLOBALS['CONN']->db_query($QValidate);
			} else {
				$QValidate = "UPDATE timeclock_xtime
				SET Status = {$STA[2]}
				WHERE ref = {$STA[1]}";
				$GLOBALS['CONN']->db_query($QValidate);
			}
		}
	}
} 

if (isset($_POST['Print'])) {
echo '<iframe src="../Templates/htck_prsndelays.php?Pr='.$_POST['Print'].'" width="0" height="0">';
echo '</iframe>';
} 
?>
<div id="PrintVault"></div>
<script charset="utf-8" type="text/javascript" src="../../../Shared/Libraries/JS/calend.js"></script> 
<table width="100%"  border="0" cellpadding="4" cellspacing="1">
  <tr>
    <td><table width="100%"  border="0" cellpadding="3" cellspacing="1">
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" name="ManageEvents" method="get" runat="server">
        <tr bgcolor="#D6D6D6">
          <td colspan="8">
		<?php
		$Cvents = "SELECT COUNT(a.ref) AS No, b.name AS Type 
		  			FROM timeclock_delays AS a 
		  				INNER JOIN htck_types AS b ON b.StatRef = a.refType AND b.Type = 2
		  				WHERE a.Status < 3
						GROUP BY a.refType";
		$Events = $GLOBALS['CONN']->db_array($Cvents);
		for ($r = 1; $r <= $Events[0]; $r++) {
		 echo '('.$Events[$r]['No'].') '.$Events[$r]['Type'].' '; 
		 } 
		$Cvents = "SELECT COUNT(a.ref) AS No, b.name AS Type 
		  			FROM timeclock_xtime AS a 
		  				INNER JOIN htck_types AS b ON b.StatRef = a.refType AND b.Type = 2
		  				WHERE a.Status < 3
						GROUP BY a.refType";
		$Events = $GLOBALS['CONN']->db_array($Cvents);
		for ($r = 1; $r <= $Events[0]; $r++) {
		 echo '('.$Events[$r]['No'].') '.$Events[$r]['Type'].' '; 
		 } ?></tr>
        <tr bgcolor="#99CCCC">
          <td colspan="8" bgcolor="#ACB1CC">Ver:
		 <?php 
		  	$CList = "SELECT * FROM htck_types WHERE Type = 2";
			$List = $GLOBALS['CONN']->db_array($CList);
			for ($r = 1; $r <= $List[0]; $r++) {
			echo '<input name="EventType" type="radio" value="'.$List[$r]['StatRef'].'" ';
			if (isset($_GET['EventType']) && $_GET['EventType'] == $List[$r]['StatRef']) { echo 'checked';}
            echo '> '.$List[$r]['name'];
			}
			echo '<input name="Event" type="checkbox" id="Event" value="Open" ';
			if (!isset($_GET['EventType']) || isset($_GET['Event'])) { echo 'checked'; }
			echo '>
			Ver apenas nÃo resolvidas';?></tr>
        <tr bgcolor="#99CCCC">
          <td bgcolor="#ACB1CC"> 
              </select>
	<input type="submit" name="Consultar" value="Consultar"></td>
          <td bgcolor="#ACB1CC">
<?php
    echo '<select name="Offices">
    <option value="">Todos</option>';
	      
	$QCallOffice = "SELECT ref, name FROM main_offices WHERE ref_domain = '1' ORDER BY address_city ASC";
	$CallOffice = $GLOBALS['CONN']->db_array($QCallOffice); 
	for ($o = 1; $o <= $CallOffice[0]; $o++) {
	echo '<option value="'.$CallOffice[$o]['ref'].'" ';
	if (isset($_GET['Offices']) && $_GET['Offices'] == $CallOffice[$o]['ref']) {echo 'selected';}
	echo '>'.$CallOffice[$o]['name'].'</option>';
    }
	echo '</select>';  ?></td>
          <td bgcolor="#ACB1CC">
<?php    
	if (!isset($_GET['Month'])) { 
		$ScdTime = date("Y-m-d H:i:s");
	} else {
		$ScdTime = date("Y-{$_GET['Month']}-d H:i:s");
	}
	$ConsultTime = split("-",$ScdTime);
	scdconsult($ScdTime,'ManageEvents');
?></td>
          <td bgcolor="#ACB1CC">&nbsp;</td>
          <td bgcolor="#ACB1CC">&nbsp;</td>
          <td bgcolor="#ACB1CC">&nbsp;</td>
          <td bgcolor="#ACB1CC">&nbsp;</td>
          <td bgcolor="#ACB1CC"><?php
	echo '<select name="Status" id="Status"><option value="">Todos</option>'; 
	$CList = "SELECT * FROM htck_status WHERE Type = {$_GET['EventType']}";
	$List = $GLOBALS['CONN']->db_array($CList);
	for ($r = 1; $r <= $List[0]; $r++) {
	echo '<option value="'.$List[$r]['StatRef'].'"';
	if (isset($_GET['Status']) && $_GET['Status'] == $List[$r]['StatRef']) { echo 'selected';}
	echo '>'.$List[$r]['name'].'</option>';
	} ?></td>
        </tr>
	</form>
	<form name="EditEvents" method="post" action="?" runat="server">
	<input name="FlStatus" value="<?php echo $_GET['Status'];?>" type="hidden">
	<input name="Month" value="<?php echo $_GET['Month'];?>" type="hidden">
	<input name="FlOffice" value="<?php echo $_GET['Offices'];?>" type="hidden">
	<input name="FlType" value="<?php echo $_GET['EventType'];?>" type="hidden">
	<input name="Event" value="<?php echo $_GET['Event'];?>" type="hidden">
    <tr bgcolor="#F2F2F2">
          <td width="20%"><div align="center">Funcionário</div></td>
          <td width="18%"><div align="center">Departamento</div></td>
          <td width="15%"><div align="center">Dia</div></td>
          <td width="10%"><div align="center">Duração</div></td>
          <td width="50%"><div align="center">Justificação</div></td>
          <td width="6%">Compensação</td>
          <td width="10%"><div align="center">(P)</div></td>
          <td width="10%"><div align="center">Estado</div></td>
     </tr>
<?php
   	    if (isset($_GET['Offices']) && $_GET['Offices'] != "") { 
			$Offices = " AND m.ref = {$_GET['Offices']}";
			} else {
			$Offices = "";
			}
		if (isset($_GET['Event']) && $_GET['Event'] == "Open") {
			$Status = " AND a.Status != 6"; 
	    } elseif (isset($_GET['Status']) && $_GET['Status'] != "") { 
			$Status = " AND a.Status = {$_GET['Status']}";
			} else {
			$Status = '';
			}
	if ($_GET['EventType'] == 4) {
	
    $QNavTime = "SELECT 
					a.ref,
					a.RefType,
					c.delaytime,
					a.Time AS xtime,
					DATE(a.Time) AS Day,
					TIME(a.EventTime) AS Time,
					a.Justification AS Justify,
					a.Status, 
					n.name,
					m.name AS office
				FROM timeclock_xtime AS a
				LEFT OUTER JOIN timeclock_operations AS b ON a.refOperation = b.ref
				LEFT OUTER JOIN timeclock_delays AS c ON c.refOperation = b.ref
				INNER JOIN main_users AS n ON n.ref = a.RefUser
				INNER JOIN main_offices AS m ON n.ref_office = m.ref
				WHERE a.RefType = {$_GET['EventType']}
				AND MONTH(a.Time) = {$ConsultTime[1]} 
				AND YEAR(a.Time) = {$ConsultTime[0]}
				{$Status}
				{$Offices}";
	
	} elseif ($_GET['EventType'] != 4) {

    $QNavTime = "SELECT 
					a.ref,
					a.RefType,
					a.delaytime,
					c.Time AS xtime,
					DATE(a.EventTime) AS Day,
					TIME(a.delaytime) AS Time,
					d.name AS Justify,
					a.Status, 
					n.name,
					m.name AS office
				FROM 
				timeclock_delays AS a
				LEFT OUTER JOIN timeclock_operations AS b ON a.refOperation = b.ref
				LEFT OUTER JOIN timeclock_xtime AS c ON c.refOperation = b.ref
				LEFT OUTER JOIN htck_delaytypes AS d ON a.Justify = d.ref
				INNER JOIN main_users AS n ON n.ref = a.main_user_ref
				INNER JOIN main_offices AS m ON n.ref_office = m.ref
				WHERE a.RefType = {$_GET['EventType']}
				AND MONTH(a.EventTime) = {$ConsultTime[1]} 
				AND YEAR(a.EventTime) = {$ConsultTime[0]}
				{$Status}
				{$Offices}";
	} 

	$NavTime = $GLOBALS['CONN']->db_array($QNavTime); 
	for ($f = 1; $f <= $NavTime[0]; $f++) { ?>
        <tr bgcolor="#F8F8F8">
          <td>
            <?php echo $NavTime[$f]['name'];?></td>
          <td><?php echo $NavTime[$f]['office'];?></td>
          <td><?php echo $NavTime[$f]['Day'];?></td>
          <td><?php echo date("H:i:s",$NavTime[$f]['delaytime']-3600);?></td>
          <td><?php 
		 echo $NavTime[$f]['Justify'];
		if (isset ($NavTime[$f]['File']) && $NavTime[$f]['File'] != '') { 
			$Comp = split('.',$NavTime[$f]['File']);
			echo '<br>Comprovativo: <a 

href="'.$GLOBALS['DOC'].'TimeClock'.$NavTime[$f]['File'].'">'.$NavTime[$f]['File'].'</a>';
		} ?></td>
          <td>
            <?php
			$BTime = $NavTime[$f]['xtime']-$NavTime[$f]['delaytime'];
			if ($BTime < 0) {
			$BTime = 0-$BTime;
			echo '<font color="#ff0000">';
			} elseif ($BTime > 0) {
				echo '<font color="#00ff00">';
			}
			echo date("H:i:s",$BTime-3600).'</font>'; ?></td>
          <td><input name="Print[]" type="checkbox" id="Print[]" value="<?php echo $NavTime[$f]['ref'];?>"></td>
          <td><?php
		  echo '<select name="Status[]" id="Status">
		  <option ';
		  if ($NavTime[$f]['Status'] == 1 || $NavTime[$f]['Status'] == 2) { 
		  	echo "selected";
			}
		  echo '></option>';
		  $CList = "SELECT * FROM htck_status WHERE StatRef IN (3, 6, 5) AND Type = {$_GET['EventType']}";
		  $List = $GLOBALS['CONN']->db_array($CList);
		  for ($r = 1; $r <= $List[0]; $r++) {
		  echo '<option value="'.$NavTime[$f]['RefType'].'-'.$NavTime[$f]['ref'].'-'.$List[$r]['StatRef'].'" ';
		  if ($NavTime[$f]['Status'] == $List[$r]['StatRef']) { 
		  	echo "selected";
			} 
		  echo '>'.$List[$r]['name'].'</option>';
		  } 
		  echo '</select>'; ?></td>
        </tr>
    <?php } ?>
		<tr>
		<td><input name="Save" type="submit" id="Save" value="Salvar">
		</td></tr>
      </form>
    </table>
    </td>
  </tr>
</table>
<?php echo footer();?>